package com.hopu.shop.server.imp;


import com.hopu.shop.dao.SupportDao;
import com.hopu.shop.server.ShopInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("shopInfoService")
@CacheConfig(cacheNames = {"myCache"})
public class ShopInfoServiceImp implements ShopInfoService {


    @Autowired
    SupportDao supportDao;

    /**
     * 商品信息的查询
     * @param map
     * @return
     */
    @Override
    @CachePut(key = "targetClass + methodName +#p0")
    public List<?> query(Map map) throws Exception {
        return supportDao.query("ShopInfoMapper.query",map);
    }


    /**
     * 修改
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    @CachePut(key = "targetClass + methodName +#p0")
    public int upd(Map map) throws Exception {
        try {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time_date = sdf.parse(sdf.format(date));
            System.out.println("time_date:"+time_date);
            map.put("modify_time",time_date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //maps循环遍里出来是判断条件 map是我的修改条件
        Map maps=new HashMap();
        maps.put("keyMap",map);
        maps.put("shop_info_id",map.get("shop_info_id"));
        map.remove("shop_info_id");
        maps.put("version",map.get("version"));
        map.put("version",Integer.valueOf(map.get("version")+"")+1);
        System.out.println("maps="+maps);
        System.out.println("map="+map);
        return supportDao.upd("ShopInfoMapper.upd",maps);
    }


    /**
     * 新增
     * @param str
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    @CachePut(key = "targetClass + methodName +#p0")
    public int sava(Map map) throws Exception {
        System.out.println("业务层的map="+map);
        return supportDao.sava("ShopInfoMapper.save",map);
    }


    /**
     * 删除
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    @CachePut(key = "targetClass + methodName +#p0")
    public int del(Map map) throws Exception {
        return supportDao.del("ShopInfoMapper.del",map);
    }



    @Override
    public Map inofcount(Map map) throws Exception {
        return supportDao.inofcount("ShopInfoMapper.inofcount",map);
    }
}
